package com.example.chengzhong.config;

import com.example.chengzhong.DTO.UserList;
import com.example.chengzhong.controller.ComPortReader;
import com.example.chengzhong.mapper.UtilMapper;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class PostConstructUtil {

    @Autowired
    private UtilMapper utilMapper;

    public static List<UserList> userLists;

    public static Map<String, String> getUserMap() {
        Map<String, String> userMap = new HashMap<>();
        List<UserList> userLists = PostConstructUtil.userLists;
        for (UserList user : userLists) {
            String userId = user.getUserid();
            if (userMap.containsKey(user.getName())) {
                userId = userId + "|" + userMap.get(user.getName());
                userMap.put(user.getName(), userId);
            }
            userMap.put(user.getName(), userId);
        }
        return userMap;
    }

    private static final String PORT_NAME = "COM1";
    private static final int BAUD_RATE = 1200;
    private static final int DATA_BITS = SerialPort.DATABITS_8;
    private static final int STOP_BITS = SerialPort.STOPBITS_1;
    private static final int PARITY = SerialPort.PARITY_NONE;

    private static SerialPort serialPort;
    private static InputStream inputStream;

    private static StringBuilder sb = new StringBuilder();
    @Bean
    InitializingBean initializingBean() {
        return () -> {
//            Date date = new Date();
//            Timestamp timestamp = new Timestamp(date.getTime());
//            long start = System.currentTimeMillis();
//            newGetAllUser();
//            long end = System.currentTimeMillis();
//            System.out.println(timestamp + ":" + (end - start)/1000 +"秒");
            log.warn("PostConstructUtil启动启动启动启动启动启动启动启动启动");

//            try {
//                // 1. 获取串口标识
//                CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(PORT_NAME);
//
//                // 2. 打开串口并设置参数
//                serialPort = (SerialPort) portIdentifier.open("ComReader", 2000);
//                serialPort.setSerialPortParams(BAUD_RATE, DATA_BITS, STOP_BITS, PARITY);
//
//                // 3. 注册事件监听器
//                serialPort.addEventListener(new SerialPortListener());
//                serialPort.notifyOnDataAvailable(true);
//
//                // 4. 获取输入流
//                inputStream = serialPort.getInputStream();
//
//                System.out.println("串口连接成功，开始监听数据...");
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        };
    }

    private void newGetAllUser() {
        userLists = utilMapper.selectQYWXAllUser();
    }


}
